<template>
  <div class="app-container">
    <!-- 搜索表单 -->
    <el-form
      ref="queryRef"
      :inline="true"
      label-width="68px"
      :model="queryParams"
    >
      <el-form-item label="名称" prop="name">
        <el-input
          placeholder="请输入名称"
          v-model="queryParams.name"
          clearable
        />
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="Search" @click="handleQuery"
          >搜索</el-button
        >
        <el-button icon="Refresh" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <!-- 功能按钮栏 -->
    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button type="primary" plain icon="Plus" @click="handleAdd"
          >新增</el-button
        >
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="Delete"
          @click="handleDelete"
          :disabled="multiple"
          >删除</el-button
        >
      </el-col>

      <!-- 功能按钮栏 -->
      <right-toolbar
        v-model:showSearch="showSearch"
        @queryTable="getList"
      ></right-toolbar>
    </el-row>

    <!-- 数据展示表格 -->
    <el-table
      v-loading="loading"
      :data="stationList"
      @selection-change="handleSelectionChange"
    >
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column prop="imageUrl" label="站点图片" #default="scope" width="80">
        <img :src="scope.row.imageUrl" width="60" />
      </el-table-column>
      <el-table-column label="站点名称" prop="name" width="130"/>
      <el-table-column label="营业时间" prop="businessHours" width="130"/>
      <el-table-column label="详细地址" prop="fullAddress" />
      <el-table-column label="负责人名称" prop="headName" width="90"/>
      <el-table-column label="负责人电话" prop="headPhone" width="130"/>
      <el-table-column label="关联柜机" prop="cabinetNo" />
      <el-table-column label="费用规则" prop="feeRuleName" />
      <el-table-column label="状态" prop="status" width="80">
        <template #default="scope">
          {{ scope.row.status == '1' ? '正常' : '停用' }}
        </template>
      </el-table-column>
      <el-table-column
        label="操作"
        align="center"
        class-name="small-padding fixed-width"
      >
        <template #default="scope">
          <el-button
            link
            type="primary"
            icon="Edit"
            @click="handleUpdate(scope.row)"
            >修改</el-button
          >
          <el-button
            link
            type="primary"
            icon="Delete"
            @click="handleDelete(scope.row)"
            >删除</el-button
          >
        </template>
      </el-table-column>
    </el-table>

    <!-- 分页条组件 -->
    <pagination
      v-show="total > 0"
      :total="total"
      v-model:page="queryParams.pageNum"
      v-model:limit="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改站点对话框 -->
    <el-dialog :title="title" v-model="open" width="50%" append-to-body>
      <el-form
        ref="stationRef"
        :model="form"
        :rules="rules"
        label-width="120px"
      >
        <el-form-item label="站点名称" prop="name">
          <el-input v-model="form.name" placeholder="请输入站点名称" />
        </el-form-item>
        <!-- <el-form-item label="站点图片" prop="logo">
          <el-upload
            class="avatar-uploader"
            :action="imgUpload.url"
            :headers="imgUpload.headers"
            :show-file-list="false"
            :on-success="handleAvatarSuccess"
          >
            <img v-if="form.imageUrl" :src="form.imageUrl" class="avatar" />
            <el-icon v-else class="avatar-uploader-icon"><Plus /></el-icon>
          </el-upload>
        </el-form-item> -->
        <el-form-item label="营业时间" prop="businessHours">
          <el-input v-model="form.businessHours" placeholder="请输入营业时间" />
        </el-form-item>
        <el-form-item label="省市区" prop="categoryIdList">
          <el-cascader
            :props="regionProps"
            v-model="form.regionCodeList"
            style="width: 100%"
          />
        </el-form-item>
        <el-form-item label="详细地址" prop="address">
          <el-input v-model="form.address" placeholder="请输入详细地址" />
        </el-form-item>
        <el-form-item label="设置经纬度">
          <el-input
            v-model="latLngString"
            disabled
            placeholder="选择经纬度"
            style="width: 70%"
          />
          <el-button type="primary" icon="Search" @click="handShowMap"
            >选择经纬度</el-button
          >
        </el-form-item>
        <el-form-item label="站点负责人名称" prop="headName">
          <el-input
            v-model="form.headName"
            placeholder="请输入站点负责人名称"
          />
        </el-form-item>
        <el-form-item label="站点负责人电话" prop="headPhone">
          <el-input
            v-model="form.headPhone"
            placeholder="请输入站点负责人电话"
          />
        </el-form-item>
      </el-form>
      <template #footer>
        <div class="dialog-footer">
          <el-button type="primary" @click="submitForm">确 定</el-button>
          <el-button @click="cancel">取 消</el-button>
        </div>
      </template>
    </el-dialog>
  </div>
</template>
<script setup name="station">
import { ref, reactive } from "vue";

import {
  getStationList,
  getStationDetail,
  addStation,
  updateStation,
  deleteStation,
  setData,
} from "@/api/device/station";
import { searchNoUseList } from "@/api/device/cabinet";
import { getTreeSelect } from "@/api/device/region";

//定义分页列表数据模型
const stationList = ref([]);
//定义列表总记录数模型
const total = ref(0);
//加载数据时显示的动效控制模型
const loading = ref(false);
//添加框
const open = ref(false);
const title = ref("");

const data = reactive({
  //自定义搜索模型
  queryParams: {
    name: null, //名称
    pageNum: 1, //页码
    pageSize: 10, //每页显示的条数
  },
  //定义表单模型
  form: {},
  //定义表单校验规则
  rules: {
    name: [{ required: true, message: "请输入名称", trigger: "blur" }],
  },
});
const { form, queryParams, rules } = toRefs(data);

const { proxy } = getCurrentInstance();

//分页列表
function getList() {
  loading.value = true; //显示加载效果
  getStationList(data.queryParams).then((response) => {
    stationList.value = response.rows; //设置分页列表数据
    total.value = response.total; //设置列表总记录数
    loading.value = false; //隐藏加载效果
  });
}

//新增
function handleAdd() {
  reset();
  getRegionList();
  open.value = true;
  title.value = "添加站点";
}

//修改
function handleUpdate(row) {
  reset();
  const _id = row.id;
  getStationDetail(_id).then((response) => {
    form.value = response.data;
    open.value = true;
    title.value = "修改站点";
  });
}

//表单提交按钮
function submitForm() {
  proxy.$refs["stationRef"].validate((valid) => {
    if (form.value.id != null) {
      updateStation(form.value).then((response) => {
        proxy.$modal.msgSuccess("修改成功");
        open.value = false;
        getList();
      });
    } else {
      addStation(form.value).then((response) => {
        proxy.$modal.msgSuccess("新增成功");
        open.value = false;
        getList();
      });
    }
  });
}


//搜索列表
const showSearch = ref(true);
function handleQuery() {
  getList();
}
/** 重置按钮操作 */
function resetQuery() {
  proxy.resetForm("queryRef");
  handleQuery();
}

//批量删除
const ids = ref([]);
//定义单选控制模型
const single = ref(true);
//定义多选控制模型
const multiple = ref(true);
//多选框选中数据
function handleSelectionChange(selection) {
  ids.value = selection.map((item) => item.id);
  single.value = selection.length != 1;
  multiple.value = !selection.length;
}


// 删除按钮操作
function handleDelete(row) {
  const _ids = row.id || ids.value;
  ElMessageBox.confirm(
    "是否确认删除国际类型编号为：" + _ids + "的数据项？",
    "系统提示",
    {
      confirmButtonText: "确认",
      cancelButtonText: "取消",
      type: "warning",
    }
  )
    .then(function () {
      return deleteStation(_ids);
    })
    .then(() => {
      getList();
      proxy.$modal.msgSuccess("删除成功");
    })
    .catch(() => {});
}

// 取消按钮
function cancel() {
  open.value = false;
  reset();
}

//表单重置
function reset() {
  form.value = {
    id: null,
    name: null,
    imageUrl: null,
    businessHours: null,
    categoryIdList: null,
    address: null,
    latLngString: null,
    headName: null,
    headPhone: null,
  };
  proxy.resetForm("cabinetTypeRef");
}

import { getAllInfo } from "@/api/device/cabinet";
const openCabinet = ref(false);
const cabinet = ref({});
const cabinetSlotList = ref([]);
function handleCabinetShow(row) {
  form.value = row;
  getAllInfo(row.id).then(response => {
    cabinet.value = response.data.cabinet;
    cabinetSlotList.value = response.data.cabinetSlotList;
    openCabinet.value = true;
  });
}
function cancelCabinet() {
  openCabinet.value = false;
}


//地区列表
const categoryIdList = ref([])
function getRegionList() {
  const provinceCode = ''
  getTreeSelect(provinceCode).then(response => {
    categoryIdList.value = response.data;
  });
}




getList();

</script>
<style scoped>
.avatar-uploader .avatar {
  width: 178px;
  height: 178px;
  display: block;
}
</style>
<style>
.avatar-uploader .el-upload {
  border: 1px dashed var(--el-border-color);
  border-radius: 6px;
  cursor: pointer;
  position: relative;
  overflow: hidden;
  transition: var(--el-transition-duration-fast);
}

.avatar-uploader .el-upload:hover {
  border-color: var(--el-color-primary);
}

.el-icon.avatar-uploader-icon {
  font-size: 28px;
  color: #8c939d;
  width: 178px;
  height: 178px;
  text-align: center;
}
</style>
